package com.parkwalk.xxx.component.tab;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JPanel;

/**
 * @author borball
 * @date 2012-4-29
 * 
 */
public class TabPanel extends JPanel {

	private static final long serialVersionUID = 4584635186545009988L;
	private final ArrayList<Page> pageList = new ArrayList<Page>();
	private MenuPanel menuPanel;
	private Page activePage;

	public TabPanel() {
		super();
		setBackground(Color.WHITE);
		this.setLayout(new BorderLayout()); // 东南西北中的布局
		menuPanel = new MenuPanel();
		menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(menuPanel, BorderLayout.NORTH);

		menuPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(-4, -5, 0, 0));
		menuPanel.setPreferredSize(new Dimension(menuPanel.getWidth(), 31));
	}

	public void addPage(String name) {
		Page page = new Page(name, this);
		addPage(page);
	}
	
	public void addPage(Page page) {
		pageList.add(page);
		menuPanel.add(page.getMenu());
		if (pageList.size() == 1) {
			changto(page);
		}
	}
	
	public Page getPage(String name) {
		for (Page p : pageList) {
			if (p.getName().equals(name)) {
				return p;
			}
		}
		return null;
	}
	
	public void changto(Page page) {
		if (activePage != null) {
			if (activePage.equals(page))
				return;
			activePage.setSelect(false);
			this.remove(activePage.getToolbar());
		}
		activePage = page;
		page.setSelect(true);
		this.add(activePage.getToolbar());
		this.updateUI();
	}

	class MenuPanel extends JPanel {
		private static final long serialVersionUID = 6695957370126891911L;

		public void paintComponent(Graphics g) {
			// TODO:move out to configuration files
			setBackground(new Color(224, 235, 247));
			super.paintComponent(g);
			// TODO:move out to configuration files
			g.setColor(new Color(186, 201, 219));
			g.drawLine(0, this.getHeight() - 2, this.getWidth(), this.getHeight() - 2);
			g.setColor(Color.WHITE);
			g.drawLine(0, this.getHeight() - 1, this.getWidth(), this.getHeight() - 1);
		}
	}
}
